<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              v-if="isAuth('module:organization:form')"
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_organization_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>
        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:organization:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-input v-model="dataForm.keyword" :placeholder="i18n('common.please_input') + i18n('common.keyword')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-select v-model="dataForm.inquiry_publish" :placeholder="$t('common.please_select') + $t('organization.inquiry_publish')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in openStatusList" :label="v.text" :key="k" :value="v.value"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select v-model="dataForm.inquiry_receive" :placeholder="$t('common.please_select') + $t('organization.inquiry_receive')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in openStatusList" :label="v.text" :key="k" :value="v.value"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select v-model="dataForm.schedule_publish" :placeholder="$t('common.please_select') + $t('organization.schedule_publish')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in openStatusList" :label="v.text" :key="k" :value="v.value"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select v-model="dataForm.audit_status" :placeholder="$t('common.please_select') + $t('common.audit_status')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in auditList" :label="v.text" :key="k" :value="v.value"></el-option>
          </el-select>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.valid_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
          </el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70px">
          </el-table-column>

          <el-table-column prop="tax_code" :label="i18n('organization.tax_code')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('organization.info')" min-width="240">
            <template v-slot="scope">
              <dl class="table_dl">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.logo">
                    <template v-slot:error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.chinese_title }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.english_title }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column prop="register_limit" :label="i18n('organization.register_limit')" width="100">
          </el-table-column>

          <el-table-column prop="valid_time" :label="i18n('organization.valid_time')" width="100">
          </el-table-column>

          <el-table-column prop="inquiry_valid_time" :label="i18n('organization.inquiry_valid_time')" width="130">
          </el-table-column>

          <el-table-column :label="$t('organization.law_status')" width="80">
            <template v-slot="scope">
              <el-switch
                v-if="isAuth('module:organization:law:status')"
                v-model="scope.row.law_status"
                :active-value="1"
                :inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.open')"
                :inactive-text="$t('common.close')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'law_status')">
              </el-switch>
              <el-text type="danger" tag="b" v-else>
                {{ i18n('common.no_permission') }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.inquiry_publish')" width="80">
            <template v-slot="scope">
              <el-switch
                v-if="isAuth('module:organization:inquiry:publish')"
                v-model="scope.row.inquiry_publish.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.open')"
                :inactive-text="$t('common.close')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'inquiry_publish')">
              </el-switch>
              <el-text type="danger" tag="b" v-else>
                {{ i18n('common.no_permission') }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.inquiry_receive')" width="80">
            <template v-slot="scope">
              <el-switch
                v-if="isAuth('module:organization:inquiry:receive')"
                v-model="scope.row.inquiry_receive.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.open')"
                :inactive-text="$t('common.close')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'inquiry_receive')">
              </el-switch>
              <el-text type="danger" tag="b" v-else>
                {{ i18n('common.no_permission') }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.schedule_publish')" width="80">
            <template v-slot="scope">
              <el-switch
                v-if="scope.row.shipowner_id > 0 && isAuth('module:organization:schedule:publish')"
                v-model="scope.row.schedule_publish.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.open')"
                :inactive-text="$t('common.close')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'schedule_publish')">
              </el-switch>
              <el-text type="danger" tag="b" v-else>
                {{ i18n('organization.no_shipowner') }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.supplier_status')" width="100">
            <template v-slot="scope">
              <el-switch
                v-if="scope.row.supplier_status.value == 1 && isAuth('module:organization:supplier:status')"
                v-model="scope.row.supplier_status.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.open')"
                :inactive-text="$t('common.close')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'supplier_status')">
              </el-switch>
              <el-text type="danger" tag="b" v-else>
                {{ i18n('organization.no_apply') }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="$t('common.audit_status')" width="80">
            <template v-slot="scope">
              <el-text :type="scope.row.audit_status.value == 1 ? 'success' : 'danger'" tag="b">
                {{ scope.row.audit_status.text }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="290">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:organization:form')"
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_organization_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                v-if="isAuth('module:organization:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>

              <el-dropdown class="more">
                <el-button icon="More">
                  {{ i18n('common.more') }}
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item v-if="isAuth('module:organization:view')" @click="router.push({ name: 'module_organization_view', query: { id: scope.row.id } })">
                      {{ i18n('organization.view') }}
                    </el-dropdown-item>

                    <el-dropdown-item v-if="isAuth('module:organization:member:list') && scope.row.audit_status.value == 1" @click="router.push({ name: 'module_organization_member_list', query: { organization_id: scope.row.id } })">
                      {{ i18n('organization.member.info') }}
                    </el-dropdown-item>

                    <el-dropdown-item v-if="isAuth('module:organization:harbor:list') && scope.row.inquiry_receive.value == 1" @click="router.push({ name: 'module_organization_harbor_list', query: { organization_id: scope.row.id } })">
                      {{ i18n('organization.harbor.info') }}
                    </el-dropdown-item>

                    <el-dropdown-item v-if="isAuth('module:organization:contract:form') && scope.row.audit_status.value == 1" @click="router.push({ name: 'module_organization_contract_form', query: { organization_id: scope.row.id } })">
                      {{ i18n('organization.contract.info') }}
                    </el-dropdown-item>

                    <el-dropdown-item v-if="isAuth('module:organization:log:list') && scope.row.audit_status.value == 1" @click="router.push({ name: 'module_organization_log_list', query: { organization_id: scope.row.id } })">
                      {{ i18n('organization.log.info') }}
                    </el-dropdown-item>

                    <el-dropdown-item v-if="isAuth('module:organization:audit:form') && scope.row.audit_status.value != 1" @click="router.push({ name: 'module_organization_audit_form', query: { id: scope.row.id } })">
                      {{ $t('organization.audit.info') }}
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="transporter-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('transporter')

  const openStatusList = ref([])
  const auditList = ref([])

  const dataForm = reactive([
    'keyword',
    'shipowner_id',
    'inquiry_publish',
    'inquiry_receive',
    'schedule_publish',
    'audit_status',
    'valid_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadOpenStatusList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'open_status',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        openStatusList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadAuditList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'audit_status',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        auditList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    if(query.keyword) {
      dataForm.keyword = query.keyword
    }

    getDataList(model)

    loadOpenStatusList()
    loadAuditList()
  })
</script>
